package com.example.spinning.controller;

import com.example.spinning.bean.EndProductQuality;
import com.example.spinning.bean.PaginationResult;
import com.example.spinning.bean.RespBean;
import com.example.spinning.service.EndQualityService;
import com.example.spinning.utils.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/endProduct")
public class EndQualityController {
    @Autowired
    EndQualityService endQualityService;

    //增加记录
    @Log(module = "成品质量",description = "增加成品质量信息")
    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public RespBean addNewEndQuality( EndProductQuality endProductQuality) {
        if ("".equals(endProductQuality.getName())|| endProductQuality.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(endProductQuality.getBatch_no())|| endProductQuality.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        int result = endQualityService.addNewEndQuality(endProductQuality);
        if (result == 1) {
            return new RespBean("success",  "成品信息添加成功");
        } else {
            return new RespBean("error","添加成品信息成功");
        }
    }

    //根据id删除记录
    @Log(module = "成品质量",description = "删除成品质量信息")
    @RequestMapping(value = "/delete/", method = RequestMethod.DELETE)
    public RespBean deleteById(Long[] ids) {
        boolean result = endQualityService.deleteEndQualityById(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    @RequestMapping(value = "/getByMix/", method = RequestMethod.GET)
    public Map<String, Object> findPage(Integer page, Integer size,String batch_no, String name, Date date){
            PaginationResult pr = endQualityService.findPage(page, size,batch_no,name,date);
            Map<String, Object> map = new HashMap<>();
            map.put("totleCount",pr.getTotal());
            map.put("list",pr.getList());
            return map;
        }



    @Log(module = "成品质量",description = "修改成品质量信息")
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    public RespBean updateById(EndProductQuality endProductQuality){

        int result = endQualityService.updateCategoryById(endProductQuality);
        if (result == 1) {
            return new RespBean("success",  "成品信息修改成功");
        } else {
            return new RespBean("error","成品信息修改失败");
        }
    }



}

